home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 946 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.8 KB

  1. From: Ross Smith <alien@netlink.co.nz>
  2. Message-ID: <31622189.3172@netlink.co.nz>
  3. X-Original-Date: Wed, 03 Apr 1996 18:58:17 +1200
  4. Path: in1.uu.net!bounce-back
  5. Date: 03 Apr 96 08:04:22 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Rationale behind disallowal of non-const r
  9. Organization: Department of High Energy Magic, Unseen University
  10. References: <DAVEW.96Mar27195129@trigati.cs.haverford.edu> <4jevb1$kkp@engnews1.Eng.Sun.COM>
  11. X-Mailer: Mozilla 2.01 (Win95; I)
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMWIyDuEDnX0m9pzZAQELIAF9FehCOLXcU5gJtUnIluwaop7Q1Pi9TxzU
  14.     pJyi+LkDYxvujG8+u7MqX1Tx+ZftPwFX
  15.     =8CUw
  16.  
  17. Steve Clamage wrote:
  18. > Yes, there are times when you would like to be able to bind an rvalue
  19. > to a non-const reference, since the only things you care about are
  20. > sure to happen anyway. For example, given a class T:
  21. >         T f1();     // f1 returns a T rvalue
  22. >         f1().foo(); // ok only if foo is a const member function of T
  23.  
  24. I find this very surprising; perhaps I'm misunderstanding you. This seems
  25. to disallow some very useful techniques.
  26.  
  27. As an example, suppose I have a string class with some member functions:
  28.  
  29.     class String {
  30.       public:
  31.         String Substring(int start, int length) const;
  32.           // Extract a substring
  33.         const String& LowerCase();
  34.           // Convert to lower case, and return a const reference to *this
  35.     };
  36.  
  37. Are you saying I can't write something like:
  38.  
  39.     String foo, bar;
  40.     foo = bar.Substring(1, 10).LowerCase();
  41.  
  42. (i.e. the call to Substring() returns a temporary, which is then converted
  43. to lower case, and copied to foo via String::operator= before the temp is
  44. destroyed -- at least, that's what I thought would happen)
  45.  
  46. I use this sort of technique frequently, and neither of the compilers I use
  47. (Symantec C++ 7.21 and IBM Visual Age C++ 3.0) raise any objection (IBM's
  48. class library explicitly encourages this sort of thing, in fact).
  49.  
  50. Have I (and IBM and Symantec) got it wrong, or have I just misunderstood
  51. what you wrote? If my code *is* legal, could you explain how it differs
  52. from yours, which seems to be doing exactly the same thing?
  53.  
  54. -- 
  55. Ross Smith ........................................ Wellington, New Zealand
  56. Home: <mailto:alien@netlink.co.nz> ... Work: <mailto:ross.smith@nz.eds.com>
  57. "I keep my ear very close to the ground, and in consequence I listen to a
  58. lot of dog crap."                                           -- Alexei Sayle
  59. ---
  60. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  61. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  62. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  63. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  64. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  65.